import os
import shutil


def get_required_jars():
    """返回图片中需要复制的所有jar文件名"""
    return [
        "ruoyi-auth.jar",
        "ruoyi-gateway.jar",
        "ruoyi-modules-file.jar",
        "ruoyi-modules-gen.jar",
        "ruoyi-modules-job.jar",
        "ruoyi-modules-system.jar",
        "ruoyi-visual-monitor.jar",
        "selfExamFee.jar",
        "site-and-rec-stu.jar",
        "studentManager.jar",
        "student-status-and-tuition-fees.jar",
        "web-manage.jar"
    ]


def copy_specific_jars():
    """主函数：复制特定jar文件"""
    print("=== Ruoyi项目JAR文件复制工具 ===")

    # 获取用户输入路径
    source_dir = input("请输入包含jar文件的源文件夹路径: ").strip()
    target_dir = input("请输入目标文件夹路径: ").strip()

    # 验证路径
    if not os.path.isdir(source_dir):
        print(f"错误：源文件夹不存在 - {source_dir}")
        return

    # 创建目标文件夹
    os.makedirs(target_dir, exist_ok=True)
    print(f"\n目标文件夹已创建: {target_dir}")

    # 开始复制
    required_jars = get_required_jars()
    copied_count = 0

    print("\n开始搜索并复制文件...")
    for root, _, files in os.walk(source_dir):
        for file in files:
            if file in required_jars:
                src_path = os.path.join(root, file)
                dest_path = os.path.join(target_dir, file)

                # 处理重名文件
                counter = 1
                while os.path.exists(dest_path):
                    name, ext = os.path.splitext(file)
                    dest_path = os.path.join(target_dir, f"{name}_{counter}{ext}")
                    counter += 1

                shutil.copy2(src_path, dest_path)
                print(f"已复制: {src_path} → {dest_path}")
                copied_count += 1

    print(f"\n操作完成！共复制 {copied_count} 个文件")
    print(f"目标位置: {target_dir}")


if __name__ == "__main__":
    copy_specific_jars()